using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using GoogleEarthCOMWrapper;
using EARTHLib;

namespace TestGoogleEarthCOMWrapper
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                // Create the API Wrapper...this also inits it.
                GoogleEarthAPI geAPI = new GoogleEarthAPI();

                // Run some examples

                // Get Version info about your GoogleEarth Client
                int major = geAPI.VersionMajor;
                int minor = geAPI.VersionMinor;
                int build = geAPI.VersionBuild;
                string appType = geAPI.VersionAppType.ToString();
                Console.WriteLine("Version = " + major + "." + minor + "." + build + " " + appType);

                // Open a local Kml File
                string fileName = AppDomain.CurrentDomain.BaseDirectory + "\\test.kmz";
                Console.WriteLine("\nCalling OpenKmlFile(" + fileName + ", true)...");
                geAPI.OpenKmlFile(fileName, true);
                
                // Get the Camera info
                CameraInfoGEClass cam = geAPI.GetCamera(false);
                Console.WriteLine("\nCamera Info:");
                Console.WriteLine("\tAzimuth " + cam.Azimuth);
                Console.WriteLine("\tFocusPointAltitude " + cam.FocusPointAltitude);
                Console.WriteLine("\tFocusPointAltitudeMode " + cam.FocusPointAltitudeMode);
                Console.WriteLine("\tFocusPointLatitude " + cam.FocusPointLatitude);
                Console.WriteLine("\tFocusPointLongitude " + cam.FocusPointLongitude);

                // Get the ViewExtents
                ViewExtentsGEClass ve = geAPI.ViewExtents;
                Console.WriteLine("\nView Extents");
                Console.WriteLine("\tNorth " + ve.North);
                Console.WriteLine("\tSouth " + ve.South);
                Console.WriteLine("\tEast " + ve.East);
                Console.WriteLine("\tWest " + ve.West);

                // Get the lat/lon/alt at screen coord (100, 100)
                GeoPoint gp = geAPI.GetPointOnTerrainFromScreenCoords(100, 100);
                Console.WriteLine("\nGetPointOnTerrainFromScreenCoords(100, 100) = ");
                Console.WriteLine("\t(" + gp.Lat + ", " + gp.Lon + ", " + gp.AltMeters + ")");

                // Get the My Places Feature
                FeatureGEClass fc = geAPI.GetMyPlaces();
                if (fc != null)
                {
                    foreach (FeatureGE fcge in fc.GetChildren())
                    {
                        fcge.Highlight();
                        Console.WriteLine("Feature " + fcge.Name + " highlighted");
                        if (fcge.Name.Equals("Sage"))
                        {
                            fcge.Visibility = 1;
                        }
                    }
                }

                // Load some kml via a string
                //FeatureGE sage = geAPI.GetFeatureByName("Sage");
                //sage.Highlight();
                //sage.Visibility = 1;
                string doc = ReadKml("test1.kml");
                geAPI.LoadKmlData(doc);
            }
            catch (Exception e)
            {
                // If you close the GE Client before one of the API methods executes, you would get
                // an ApplicationUninitializedException for example.  There are other exceptions as well.
                Console.WriteLine(e.Message + "\n" + e.StackTrace);
            }
            Console.WriteLine("Press any key to exit...");
            Console.ReadLine();
        }

        static string ReadKml(string fileName)
        {
            StreamReader sr = new StreamReader(fileName);
            string doc = sr.ReadToEnd();
            return doc;
        }
    }
}
